package io.finer.erp.jeecg.bas.service.impl;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.finer.erp.jeecg.bas.entity.ErpZrkPiecerate;
import io.finer.erp.jeecg.bas.entity.ErpZrkQz;
import io.finer.erp.jeecg.bas.entity.ZrkFqjf;
import io.finer.erp.jeecg.bas.mapper.ErpZrkPiecerateMapper;
import io.finer.erp.jeecg.bas.service.IErpZrkPiecerateService;
import io.finer.erp.jeecg.bas.service.IErpZrkQzService;
import io.finer.erp.jeecg.bas.service.IZrkFqjfService;

/**
 * @Description: 计件工资表
 * @Author: jeecg-boot
 * @Date:   2020-10-29
 * @Version: V1.0
 */
@Service
public class ErpZrkPiecerateServiceImpl extends ServiceImpl<ErpZrkPiecerateMapper, ErpZrkPiecerate> implements IErpZrkPiecerateService {

	@Autowired
	private IErpZrkQzService qzService;
	@Autowired
	private IZrkFqjfService fqjfService;
	
	@Override
	public void addPiecerate(ErpZrkPiecerate zrkPiecerate) {
		BigDecimal total=BigDecimal.ZERO;
		//统计该条记录的合计薪资(切张计算)
//		ZrkQz zrkQz=new ZrkQz();
//		zrkQz.setQzType(zrkPiecerate.getZrkType().toString());
		//查询切张参数
		Map<String,Object> queryMap=new HashMap<String,Object>();
		queryMap.put("qz_type",zrkPiecerate.getZrkType());
		Collection<ErpZrkQz> qzList=qzService.listByMap(queryMap);
		Iterator<ErpZrkQz> iterator = qzList.iterator();
		int i=0;
		if(i==0) {
			while(iterator.hasNext()) {
				ErpZrkQz qz=iterator.next();
				//计算普通切张工资
				BigDecimal tsMoney=zrkPiecerate.getZrkTs().multiply(new BigDecimal(qz.getQzTs()));
				BigDecimal zsMoney=zrkPiecerate.getZrkQzzs().multiply(new BigDecimal(qz.getQzZs()));
				BigDecimal pfMoney=zrkPiecerate.getZrkQzpf().multiply(new BigDecimal(qz.getQzPf()));
				BigDecimal totalMoney=tsMoney.add(zsMoney).add(pfMoney);
				total=total.add(totalMoney);
				zrkPiecerate.setZrkTotalmoney(total);
			}
		}
		//分切薪资计算
//		ZrkFqjf zrkFq=new ZrkFqjf();
//		zrkFq.setMachineType(zrkPiecerate.getZrkJqbh());
		//查询分切单价参数
		if(StringUtils.isNotBlank(zrkPiecerate.getZrkJqbh())) {
			Map<String,Object> queryMapFQ=new HashMap<String,Object>();
			queryMapFQ.put("machine_type",zrkPiecerate.getZrkJqbh());
			Collection<ZrkFqjf> fqList=fqjfService.listByMap(queryMapFQ);
			int j=0;
			Iterator<ZrkFqjf> iter=fqList.iterator();
			if(j==0) {
				while(iter.hasNext()) {
					ZrkFqjf fq=iter.next();
					BigDecimal dsMoney=zrkPiecerate.getZrkFqds().multiply(new BigDecimal(fq.getDsPrice()));
					BigDecimal tdsMoney=zrkPiecerate.getZrkFqtds().multiply(new BigDecimal(fq.getTdsPrice()));
					BigDecimal fqPfMoney=zrkPiecerate.getZrkFqpf().multiply(new BigDecimal(fq.getPfPrice()));
					BigDecimal fqTotal=dsMoney.add(tdsMoney).add(fqPfMoney);
					BigDecimal totalRealMoney=total.add(fqTotal);
					zrkPiecerate.setZrkTotalmoney(totalRealMoney);//统计该条记录薪资后设置实体bean
				}
			}
		}
		
		save(zrkPiecerate);
	}

}
